package com.wu.shiro;



import com.wu.constants.Constants;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Deque;

/**
 * shiro_cache 读写规则自定义
 * （可以用redis和ehcache）
 */
public class ShiroCacheUtil {

    private Cache<String, Object> cache;
    @Resource(name = "shiroCache")
    private CacheManager cacheManager;


    @PostConstruct
    public void init() {
        this.cache = cacheManager.getCache(Constants.CODE_CACHE);
    }

    /**
     * 获取系统中同一账号已经登录的用户
     * @param principal 用户账号
     * @return
     */
    public Object getOnlineUser(String principal) {
        return cache.get(Constants.SSO_USERS+":"+principal);
    }

    /**
     * 设置系统中同一账号在系统中允许存在的sessionId列表
     * @param principal
     * @param sessionIdDeque
     * @return
     */
    public boolean setOnlineUser(String principal, Deque<Serializable> sessionIdDeque) {
        cache.put(Constants.SSO_USERS+":"+principal, sessionIdDeque);
        return true;
    }

}
